<template>
  <div class="login">
    <div class="login-title">
      <p>登录</p>
    </div>
    <div class="form-wrap">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="" prop="account">
          <span class="iconfont">&#xe653;</span>
          <el-input v-model="ruleForm.account" placeholder="请输入三个字以内用户名"></el-input>
        </el-form-item>
        <el-form-item label="" prop="pass">
          <span class="iconfont">&#xe6d7;</span>
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
            placeholder="密码"
          ></el-input>
        </el-form-item>
        <span>
          <router-link to>忘记密码?</router-link>
        </span>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import {mapMutations} from "vuex"
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("请输入三个字以内的用户名"));
      }
      setTimeout(() => {
        if (typeof value == String) {
          callback(new Error("请输入文字"));
        } else {
          if (value.length > 3) {
            callback(new Error("只能输入不超过三个汉字呦"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } 
      else if(value.length<6){
        callback(new Error("请输入至少6位"));
      }else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        account: ""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        account: [{ validator: checkAge, trigger: "blur" }]
      }
    };
  },
  methods: {
    ...mapMutations(["savetoken","savename"]),
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        let {pass,account}=this.ruleForm
        if (valid) {
          this.$axios.post("/user/login",{pass,account})
              .then(res=>{
                let data=res.data
                switch(data.code){
                  case 200:
                    console.log(data)
                    this.savetoken(data.token)
                    this.savename(data.name)
                    this.$message({
                      message: '恭喜你，登陆成功',
                      type: 'success'
                    });
                    this.$router.push("/")
                  break; 
                  case 400:
                    this.$message({
                      message: '密码错误,请重新输入',
                      type: 'warning'
                    });
                  break; 
                  case 404:
                    this.$message.error('账号不存在,请先注册');
                    this.$router.push("/auth/register")
                  break; 
                }
              })
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.login {
  //   position: relative;
  width: 360px;
  height: 400px;
  padding-top: 10px;
  background-color: white;
  margin: 100px auto 0;
  text-align: center;
  border-radius: 10px;
  transform-style: preserve-3d;
  animation: login 1s ease;
  @keyframes login {
    0% {
      transform: perspective(400px) rotateX(90deg);
      opacity: 0;
    }
    0%,
    40% {
      animation-timing-function: ease-in;
    }
    40% {
      transform: perspective(400px) rotateX(-20deg);
    }
    60% {
      transform: perspective(400px) rotateX(10deg);
      opacity: 1;
    }
    80% {
      transform: perspective(400px) rotateX(-5deg);
    }
    100% {
      transform: perspective(400px);
    }
  }
  .login-title {
    width: 300px;
    height: 100px;
    margin: -50px 0 60px 30px;
    background: rgb(220, 35, 101);
    border-radius: 3px;
    line-height: 100px;
    font-size: 20px;
    font-weight: bold;
    color: white;
    p {
      font-weight: bold;
      span{

      }
    }
  }
  .form-wrap {
    margin-top: 20px;
    .el-form-item {
      margin: 10px 50px 40px -22px;
      .el-form-item__content{
        position:relative;
        span{
          position:absolute;
          left:-45px;
          top:20px;
          font-size:25px;
          color:grey;
        }
      }
      .el-input__inner{
        border-bottom:1px solid #DCDFE6;
      }
      .el-button {
        background: rgb(220, 35, 101);
        border: none;
        width: 100px;
        height: 45px;
        border-radius: 20px;
        margin-top: 25px;
      }
    }
    span {
      float: right;
      margin: -20px 30px 0 0;
      font-size: 14px;
    }
  }
}
</style>